package com.example.user

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.LoginEntity
import com.example.base.mvi.BaseFragment
import com.example.base.mvi.HttpType
import com.example.base.mvi.UiState
import com.example.base.utils.GlideUtils
import com.example.user.databinding.FragmentMineBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import okhttp3.internal.notifyAll
import okhttp3.internal.wait

@AndroidEntryPoint
class MineFragment : BaseFragment<FragmentMineBinding,UserIntent,UserViewModel>() {
    override fun getLayout(): Int = R.layout.fragment_mine

    override fun getVM(): Class<UserViewModel> = UserViewModel::class.java

    override fun initView() {
        //viewModel.sendIntent(UserIntent.roomDetail(SPUtils.getInstance().getInt("uid")))
        GlideUtils.loadCircle(requireContext(),SPUtils.getInstance().getString("uImage",""),binding.userHead)
        binding.userName.text = if(SPUtils.getInstance().getString("username","")=="") "未登录" else SPUtils.getInstance().getString("username","")
        binding.userRole.text = if(SPUtils.getInstance().getInt("role")==20) "管理员" else "员工"

        binding.userHead.setOnClickListener {
            PictureSelector.create(this,102).selectPicture()
        }

        //切换账号 --退出当前登录账号
        binding.tvChange.setOnClickListener {
            SPUtils.getInstance().clear()
            //数据库是否需要请求
            //当前平台是否需要退出登录
            ARouter.getInstance().build("/user/LoginActivity").navigation()
        }
        //点击我的考勤，跳转到我的打卡记录界面
        binding.sign.setOnClickListener {
            ARouter.getInstance().build("/sign/SignRecordActivity").navigation()
        }
        binding.apply.setOnClickListener {
            ARouter.getInstance().build("/user/MyApplyActivity").navigation()
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> -> {
                        //val entity = it.data as LoginEntity
                        when(it.type){
                            HttpType.DEFAULT -> {
                                ToastUtils.showLong("修改头像成功")
                                val s = it.data as String
                                val last = s.split(":").last()
                                val url = "http://10.161.9.80:2209/images/headImg/${SPUtils.getInstance().getInt("uid")}/$last"
                                SPUtils.getInstance().put("uImage",url)

                            }
                           else->{}
                        }
                    }
                    else->{}
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==102&&resultCode==Activity.RESULT_OK){
            val bean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideUtils.loadCircle(requireContext(),bean.path,binding.userHead)
            //上传到服务器
            val path = bean.path
            val fileName = path.split("/").last()
            viewModel.sendIntent(UserIntent.uploadHead(path, fileName))
        }
    }

}